package com.advertising.biz;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;

import com.advertising.dao.ReleaseAddrDAO;
import com.advertising.dao.ReleaseDAO;
import com.advertising.model.CloneReleaseAddr;
import com.advertising.model.Release;
import com.advertising.model.ReleaseAddr;
import com.tgyt.common.tools.page.Pagination;
import com.tgyt.common.tools.util.DictionaryMemoryUtils;
import com.tgyt.framework.dao.hspring.DAOInterface;
import com.tgyt.framework.service.BaseService;
@Service
public class ReleaseAddrService extends BaseService<ReleaseAddr> implements
		IReleaseAddrService {
	@Resource(name="releaseDAO")
	private ReleaseDAO releaseDAO;
	
	@Resource(name="releaseAddrDAO")
	private ReleaseAddrDAO releaseAddrDAO;
	@Override
	protected DAOInterface<ReleaseAddr> getDAO() {
	 
		return releaseAddrDAO;
	}
	public int getMaxOrder(){
		return this.releaseAddrDAO.getMaxOrder();
	}
	
	public List<ReleaseAddr> getAll(){
		return this.releaseAddrDAO.getAll();
	}
	/**
	 * @Title: getPageList
	 * @Description: 具体某页显示多少条记录
	 * @param @param user 一条具体的用户信息
	 * @param @param page 当前页
	 * @param @param rows 一页显示多少条记录
	 * @param @return
	 * @return Pagination
	 * @throws
	 */
	public Pagination getPageList(ReleaseAddr release, int page, int rows,String sort,String order){
		return 	this.releaseAddrDAO.getPageList(release, page, rows, sort, order);
	}
	@Override
	public List<CloneReleaseAddr> convert(List<ReleaseAddr> releaseaddr) throws Exception {
		List<CloneReleaseAddr> clones = new ArrayList<CloneReleaseAddr>();
		if(releaseaddr!=null && releaseaddr.size()!=0){
			 DictionaryMemoryUtils dictionaryUtils = DictionaryMemoryUtils.getInstance();
			    Map maps = (Map)dictionaryUtils.getDictionarysMap().get("advertype");
			for(Iterator<ReleaseAddr> iter=releaseaddr.iterator();iter.hasNext();){
				ReleaseAddr res = iter.next();
				CloneReleaseAddr c = new CloneReleaseAddr();
				BeanUtils.copyProperties(c, res);
				if(res.getAid()!=null){
					c.setAdverstate(res.getAid().getAdverstate());
					c.setAdvertype(maps.get(res.getAid().getAdvertype()).toString());
					c.setCommentend(res.getAid().getCommentend());
					c.setCommentstart(res.getAid().getCommentstart());
					c.setCustomerNumber(res.getCustomerNumber());
					c.setIntroduction(res.getAid().getIntroduction());
					c.setNeedanalysisreport(res.getAid().getNeedanalysisreport());
				//	c.setNeedcomment(res.getAid().getNeedcomment());
					c.setPhoto(res.getAid().getPhoto());
					c.setTitle(res.getAid().getTitle());
					c.setPrice(res.getAid().getPrice());
					 
					
				}
				 
				clones.add(c);
			}
		}
		return clones;
		
	}
	@Override
	public ReleaseAddr convert(CloneReleaseAddr cloneReleaseAddr) throws Exception {
		 if(cloneReleaseAddr==null){
			 return null;
		 }
		 ReleaseAddr  releaseAddr = new ReleaseAddr();
		    BeanUtils.copyProperties(releaseAddr,cloneReleaseAddr);
		    if(cloneReleaseAddr.getAid()!=0){
		    Release release = releaseDAO.findById(cloneReleaseAddr.getAid());
		    	releaseAddr.setAid(release);
		    }
		    
		return releaseAddr;
	}

 
}
